package cn.virens.web.controller.manage.sys;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.virens.common.RequestUtil;
import cn.virens.components.page.PageParam;
import cn.virens.components.page.PageResult;
import cn.virens.database.mysql.model.sys.SysRole;
import cn.virens.database.service.sys.SysRoleService;
import cn.virens.util.Assert;
import cn.virens.web.common.BaseTableController;
import cn.virens.web.components.spring.permission.PermissionParam;

/**
 * 角色管理 - sys_role
 *
 * @author Virens
 * @since  2018-08-25 15:39:59
 */
@Controller
@RequestMapping("/manage/sys/role")
public class SysRoleContorller extends BaseTableController<SysRole, SysRoleService> {

	public SysRoleContorller() {
		super("/manage/sys/role", "sys:role");
	}

	@RequestMapping("create.jspx")
	public String createJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		return returnUrl("create");
	}

	@RequestMapping("update.jspx")
	public String updateJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("record", mBaseService.selectOne(RequestUtil.getValueInt(request, "id")));

		return returnUrl("update");
	}

	@Override
	protected PageResult<?> listHandle(HttpServletRequest request, PageParam pageParam) throws Exception {
		pageParam.addParam("identity", RequestUtil.getValueStr(request, "identity", null));
		pageParam.addParam("name", RequestUtil.getValueStr(request, "name", null));
		pageParam.addParam("id", RequestUtil.getValueInt(request, "id", null));

		return super.listHandle(request, pageParam);
	}

	@ResponseBody
	@RequestMapping("resource/get.json")
	public Model resourceGet(HttpServletRequest request, HttpServletResponse response, Model model) {
		return renderSuccess(mBaseService.getPermission(RequestUtil.getValueInt(request, "id")), model);
	}

	@ResponseBody
	@RequestMapping("resource/put.json")
	public Model resourcePut(HttpServletRequest request, HttpServletResponse response, Model model) {
		String resources = RequestUtil.getValueStr(request, "resources");
		Integer role = RequestUtil.getValueInt(request, "role");

		// 验证参数
		Assert.isEmpty(resources, "授权资源不可为空");
		Assert.isNull(role, "角色ID不可为空");

		logParam(request, "更新角色授权");

		// 授权资源转换
		JSONArray array = JSON.parseArray(resources);
		List<PermissionParam> list = new ArrayList<>();
		for (int i = 0; i < array.size(); i++) {
			JSONObject item = array.getJSONObject(i);

			PermissionParam temp = new PermissionParam();
			temp.setManage(item.getBooleanValue("manage"));
			temp.setView(item.getBooleanValue("view"));
			temp.setResource(item.getInteger("id"));
			temp.setRole(role);

			list.add(temp);
		}

		// 更新授权资源
		int result = mBaseService.putPermission(role, list);

		return renderSuccess(result, model, "授权资源已更改");
	}
}
